<template>
  <div class="padding30px">
        <!-- 搜索区域 -->
    <!-- <a-card>
      <a-form :model="searchForm" layout="inline" @submit.prevent="handleSearch">

        <a-form-item label="简称">
          <a-input v-model:value="searchForm.title" placeholder="简称" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" htmlType="submit">搜索</a-button>
        </a-form-item>

        <a-form-item>
          <a-button @click="resetSearch">重置</a-button>
        </a-form-item>
      </a-form>
    </a-card> -->
    <a-card title="查询列表">
      <!-- 操作按钮 -->
      <a-button type="primary" @click="openAddModal" style="margin-bottom: 16px">新增</a-button>
      <a-button @click="deleteSelect" style="margin-left: 16px"
        :disabled="rowSelectionSelecRow.length == 0">批量删除</a-button>
      <!-- 表格 -->
      <a-table :dataSource="dataSourceT" :columns="columns" :pagination="pagination" :row-selection="rowSelection"
        rowKey="id" bordered @change="handleTableChange">
        <!-- 自定义列渲染 -->
        <template #bodyCell="{ column, record }">
          <!-- 操作列 -->
          <template v-if="column.dataIndex === 'edit'">
            <a-button @click="openEditModal(record)">编辑</a-button>
          </template>
        </template>
      </a-table>
    </a-card>

    <!-- 新增/编辑模态框 -->
    <a-modal :title="isEditMode ? '编辑' : '新增'" :open="modalVisible" @ok="handleSubmit" @cancel="handleCancel">
      <a-form ref="formRef" :model="currentFormData" :rules="formRules" layout="vertical">

        <a-form-item label="简称" name="title">
          <a-input v-model:value="currentFormData.title" />
        </a-form-item>
        <a-form-item label="系统预设置内容" name="content">
          <a-textarea :auto-size="{ minRows: 10, maxRows: 99 }" v-model:value="currentFormData.content" />
        </a-form-item>
        <!-- <a-form-item label="创建时间" name="createTime">
          <a-input v-model:value="currentFormData.createTime" />
        </a-form-item> -->

      </a-form>
    </a-modal>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import {
  listApi,
  addApi,
  updateApi,
  updateBatchApi,
  deleteApi
} from '#/api/object/tModel.ts'


// 通用 CRUD 钩子
import { crud } from '#/components/crud'

// 图片上传组件（此处暂时未用到，保留未来扩展可能）
// import ImageUpload from '#/components/ImageUpload.vue'

// 初始化搜索表单
const initSearchForm = {
  roleName: '',
}

const pagination = {
  current: 1,
  pageSize: 10,
  total: 0,
}
// 使用 useCrud 管理数据和操作
const {
  dataSource: dataSourceT,
  searchForm,
  selectedRowKeys: rowSelectionSelecRow,
  modalVisible,
  isEditMode,
  formData: currentFormData,
  loadData,
  handleTableChange,
  handleSearch,
  resetSearch,
  deleteSelected: deleteSelect,
  openAddModal,
  openEditModal,
  handleSubmit,
  handleCancel,
} = crud({
  getList: listApi,
  create: addApi,
  update: updateApi,
  delete: deleteApi,
}, initSearchForm)

// 表单验证规则
const formRules = {
  title: [
    { required: true, message: '请输入简称' }
  ],
  content: [
    { required: true, message: '请输入系统预设置内容' }
  ],
  // createTime: [
  //   { required: true, message: '请输入创建时间' }
  // ],
}

// 表格列配置
const columns = [
  { title: 'id', dataIndex: 'id', key: 'id', align: 'center' },
  { title: '简称', dataIndex: 'title', key: 'title', align: 'center' },
  { title: '系统预设置内容', dataIndex: 'content', key: 'content', align: 'center' },
  { title: '创建时间', dataIndex: 'createTime', key: 'createTime', align: 'center' },
  { title: '操作', dataIndex: 'edit', key: 'edit', align: 'center' },
]

// 行选择配置
const rowSelection = {
  onChange: (selectedRowKeys) => {
    console.log()
    rowSelectionSelecRow.value = selectedRowKeys
  }
}

// 页面挂载时加载数据
onMounted(() => {
  loadData()
})


</script>
